<?php
/**
 * 插件组 - 第三方登录
 *
 * @see \ControllerStartupPermission <- 自定义插件组一定要自己去增加对应目录权限，否则无法进入编辑页面
 *
 * Class ControllerExtensionExtensionOauths
 */
class ControllerExtensionExtensionOauth extends Controller {
    /**
     * extension 表 type 字段 <- 不同插件组只需要修改这个值
     */
    const EXTENSION_TYPE = 'oauth';

    /**
     * 对应插件目录
     */
    const EXTENSION_DIR = 'extension/'. self::EXTENSION_TYPE .'/';

    /**
     * 对应语言 / 模板 / 权限名
     */
    const EXTENSION_TYPE_PATH = 'extension/extension/'. self::EXTENSION_TYPE;

    private $error = array();

	public function index() {
		$this->load->language(self::EXTENSION_TYPE_PATH);

		$this->load->model('setting/extension');

		$this->getList();
	}

    /**
     * 安装插件
     *
     * @throws Exception
     */
	public function install() {
		$this->load->language(self::EXTENSION_TYPE_PATH);

		$this->load->model('setting/extension');

		if ($this->validate()) {
			$this->model_setting_extension->install(self::EXTENSION_TYPE, $this->request->get['extension']);

			$this->load->model('user/user_group');

			$this->model_user_user_group->addPermission($this->user->getGroupId(), 'access', self::EXTENSION_DIR . $this->request->get['extension']);
			$this->model_user_user_group->addPermission($this->user->getGroupId(), 'modify', self::EXTENSION_DIR . $this->request->get['extension']);

			$this->load->controller(self::EXTENSION_DIR . $this->request->get['extension'] . '/install');

			$this->session->data['success'] = $this->language->get('text_success');
		}

		$this->getList();
	}

    /**
     * 卸载插件
     *
     * @throws Exception
     */
	public function uninstall() {
		$this->load->language(self::EXTENSION_TYPE_PATH);

		$this->load->model('setting/extension');

		if ($this->validate()) {
			$this->model_setting_extension->uninstall(self::EXTENSION_TYPE, $this->request->get['extension']);

			$this->load->controller(self::EXTENSION_DIR . $this->request->get['extension'] . '/uninstall');

			$this->session->data['success'] = $this->language->get('text_success');
		}

		$this->getList();
	}

	protected function getList() {
		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}

		if (isset($this->session->data['success'])) {
			$data['success'] = $this->session->data['success'];

			unset($this->session->data['success']);
		} else {
			$data['success'] = '';
		}

		$this->load->model('setting/extension');

		$extensions = $this->model_setting_extension->getInstalled(self::EXTENSION_TYPE);

		foreach ($extensions as $key => $value) {
            if (! $this->hasController($value)) {
				$this->model_setting_extension->uninstall(self::EXTENSION_TYPE, $value);
				unset($extensions[$key]);
			}
		}

		$data['extensions'] = array();
		
		// Compatibility code for old extension folders
		$files = glob(DIR_APPLICATION . 'controller/'. self::EXTENSION_DIR .'*.php');
		if ($files) {
			foreach ($files as $file) {
				$extension = basename($file, '.php');

				$this->load->language(self::EXTENSION_DIR . $extension, 'extension');

				$data['extensions'][] = array(
					'name'       => $this->language->get('extension')->get('heading_title'),
					'status'     => $this->config->get(self::EXTENSION_TYPE .'_' . $extension . '_status') ? $this->language->get('text_enabled') : $this->language->get('text_disabled'),
					'sort_order' => $this->config->get(self::EXTENSION_TYPE .'_' . $extension . '_sort_order'),
					'install'    => $this->url->link(self::EXTENSION_TYPE_PATH. '/install', 'user_token=' . $this->session->data['user_token'] . '&extension=' . $extension),
					'uninstall'  => $this->url->link(self::EXTENSION_TYPE_PATH. '/uninstall', 'user_token=' . $this->session->data['user_token'] . '&extension=' . $extension),
					'installed'  => in_array($extension, $extensions),
					'edit'       => $this->url->link(self::EXTENSION_DIR . $extension, 'user_token=' . $this->session->data['user_token'])
				);
			}
		}

		$this->response->setOutput($this->load->view(self::EXTENSION_TYPE_PATH, $data));
	}

	protected function validate() {
		if (!$this->user->hasPermission('modify', self::EXTENSION_TYPE_PATH)) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		return !$this->error;
	}

    /**
     * @param $value
     *
     * @return bool
     */
    protected function hasController($value)
    {
        return is_file(DIR_APPLICATION . 'controller/' . self::EXTENSION_DIR . $value . '.php') ||
            is_file(DIR_APPLICATION . 'controller/' . self::EXTENSION_TYPE . '/' . $value . '.php');
    }
}